package com.hetongxue.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 七牛云工具类
 * @ClassNmae: QiNiuTools
 * @Author: 何同学
 * @DateTime: 2022-03-29 22:59
 **/
@Slf4j
public class QiNiuUtil {

    private static final String accessKey = "zgAqIac4iG9O6KfvHXjvZASgNOXm1hGH0wJadO3G";
    private static final String secretKey = "dMGl6UD5_ZEi0WUha6xlgkR9xVnOWqUES7zII8BY";
    private static final String bucketName = "hetongxue-blog";
    private static final String url = "r9iiyxjdn.hd-bkt.clouddn.com";


    // 获取token
    private static String getToken() {
        return getAuth().uploadToken(bucketName, null, 3600, new StringMap().put("insertOnly",
                1));
    }

    // 获取Auth
    private static Auth getAuth() {
        return Auth.create(accessKey, secretKey);
    }

    // 设置地区
    private static Configuration setRegion() {
        return new Configuration(Region.huadong());
    }

    // 获取资源管理
    private static BucketManager getBucketManager() {
        return new BucketManager(getAuth(), setRegion());
    }

    // 文件上传
    public static boolean uploadFile(String filePath) {
        Configuration cfg = setRegion();
        cfg.useHttpsDomains = false;
        try {
            Response response = new UploadManager(cfg).put(filePath, "zzz", getToken());
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            log.info("上传成功!\n文件名:" + putRet.key);
            return true;
        } catch (QiniuException e) {
            System.err.println(e.response.toString());
            log.error("上传文件失败!");
        }
        return false;
    }

    // 获取文件信息
    public static FileInfo getFileInfo(String fileName) {
        BucketManager bucketManager = getBucketManager();
        try {
            FileInfo fileInfo;
            fileInfo = bucketManager.stat(bucketName, fileName);
            return fileInfo;
        } catch (QiniuException e) {
            System.err.println(e.response.toString());
            log.error("获取文件" + fileName + "信息失败!");
        }
        return null;
    }

    // 删除文件
    public static void deleteFile(String fileName) {
        BucketManager bucketManager = getBucketManager();
        try {
            bucketManager.delete(bucketName, fileName);
        } catch (QiniuException e) {
            System.err.println(e.response.toString());
            log.error("删除文件" + fileName + "失败!");
        }
    }

    // 获取文件列表
    public static void getFileList() {
        BucketManager bucketManager = getBucketManager();
        int limit = 1000;// 迭代最大长度
        // 文件名前缀
        String prefix = "";
        // 指定目录分隔符
        String delimiter = "";
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucketName, prefix, limit, delimiter);
        FileInfo[] items = fileListIterator.next();
        for (FileInfo item : items) {
            System.out.println(item.key);
            System.out.println(item.hash);
            System.out.println(item.fsize);
            System.out.println(item.mimeType);
            System.out.println(item.putTime);
            System.out.println(item.endUser);
        }
    }
}